package com.loiot.baqi.service.job;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.loiot.baqi.commons.message.email.EmailClient;
import com.loiot.baqi.commons.message.email.SimpleEmailVo;
import com.loiot.baqi.constant.ApplicationConst;
import com.loiot.baqi.constant.Const;
import com.loiot.baqi.dao.ZpCompanyJobInfoDao;

import com.loiot.baqi.status.HeartbeatJobType;
import com.loiot.baqi.status.PauseStartType;

import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.StringUtil;

@Service("checkJobKeyword")
@Scope("prototype")
public class CheckJobKeyword extends JobTaskDefine {

	@Resource
	private ZpCompanyJobInfoDao zpCompanyJobInfoDao;
	
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	@Override
	public void doTask() {
		System.out.println(this);
		HashMap<String,Object> pmap = new HashMap<String,Object>();
		pmap.put("isDelete", PauseStartType.START.getCode());
		pmap.put("qtype",Const.SIX);
		//pmap.putAll(this.getDateTimeParam());
		try {
			
			
			List<HashMap<String, Object>> list=zpCompanyJobInfoDao.statisticsPlatformData(pmap);
			Map<String, Object>  map = this.getEmails(list.get(0));
			StringBuffer buff = new StringBuffer();

			SimpleEmailVo vo = new SimpleEmailVo();
			//发送邮件
			for (String key : map.keySet()) {
				   String keyValue= String.valueOf(map.get(key));
				   String realName  =keyValue.split(",")[0];
				   String email  =keyValue.split(",")[1];
				   String count  =keyValue.split(",")[2];
   				   String mess = ApplicationConst.getMessage("10107", realName,count,key);
   				   buff.append(mess+" \n<br>");
   				   //线上环境
   				   if(ApplicationConst.ENVIRONMENT_TYPE.equals(Const.ONLINE_ENVIRONMENT)){
   					 vo.addEmail(email);
   				   }
			}
			   System.out.println("buff:"+buff);
			//测试环境
			if(ApplicationConst.ENVIRONMENT_TYPE.equals(Const.BETA_ENVIRONMENT)){
				  vo.addEmail("273035735@qq.com");
					vo.setTitle("憬仪通知");
			        vo.setContent(buff.toString());
			        super.emailClient.sendT(vo);
			        log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT));	  
			}else {
				vo.setTitle("憬仪通知");
		        vo.setContent(buff.toString());
				super.emailClient.sendT(vo);
			    log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT));	  
			}
	        		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.exceptionContent=e.getMessage();
		}finally{
			//添加心跳
			this.jobType=(int)HeartbeatJobType.CHECK_JOB_KEYWORD.getCode();
			this.addHeartbeat();
		}
		
	}

	public Map<String ,Object>  getEmails(HashMap<String, Object> obj){
		Map<String ,Object> mails=new HashMap<String,Object>();

		int javaCount =this.getCountValue(obj, "javaCount");
		int phpCount =this.getCountValue(obj, "phpCount");
		int androidCount =this.getCountValue(obj, "androidCount");
		
		if(javaCount>0){
			//759964600@qq.com
			mails.put("java" ,"田亚涛,759964600@qq.com,"+javaCount );
		}
		if(phpCount>0){
			mails.put("php" ,"任琴,1009841887@qq.com,"+phpCount );
		}
		if(androidCount>0){
			mails.put("android" ,"旭东,420656001@qq.com,"+androidCount );
		}
		return mails;
	}
	
	
	
	public HashMap<String, Object> getDateTimeParam(){
		HashMap<String, Object> pMap = new HashMap<String, Object>();
    	pMap.put("startTime",DateUtil.addDays(new Date(), -3));
		pMap.put("endTime",new Date());
    	return pMap;
	}
	
	public int  getCountValue(HashMap<String, Object> mapObj,String key){
		if(mapObj==null || mapObj.get(key)==null){
			return 0;
		}
		return Integer.parseInt(mapObj.get(key).toString());
	}
	
	
	

}
